<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>组件内的守卫</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
    <div id="app">
        <router-link to="/one">one组件</router-link>
        <router-link to="/two">two组件</router-link>
        <router-view></router-view>
    </div>
    <script>
        /* 
            历史模式：路由器实际网页结构，当在url上手动输入组件名时，找不到页面，出现404
            原理：
            怎么解决


        
        
        */
        const MyOne={
            template:`<div>组件一</div>`,
            beforeRouteEnter (to, from, next) {
                console.log(to);
                console.log(from);
                // 如果要调用组件的对象在next中拿取
                next((vm)=>{
                    console.log(vm);
                });
            },
            beforeRouterUpdate(to,from,next){
                console.log(to,from,next);
                console.log(to);
                console.log(from);
                console.log(this);
                next();
            }
        };


        const MyTwo={template:`<div>组件二</div>`};

        const routes = [
            {
                path:"/one",
                component:MyOne,
            },
            {
                path:"/two",
                component:MyTwo,
                meta:{
                    needLosgin:true
                }
            },
            
        ];

        const router = new VueRouter({
            routes,
        });

        router.beforeEaah((to,from,next)=>{
            console.log(to);
            console.log(from);
            if(to.meta.needLogin){
                console.log("检验是否登录，假如没有登录");
            }
            next();
        })


        const app = new Vue({
            el:"#app",
            router,
        })
    </script>
</body>
</html>
